function loadPostsByCategory(categoryId, userId, type, page, pageSize, keyword) {
    var url = "/postServlet?type=" + type + "&categoryId=" + categoryId + "&userId=" + userId + "&page=" + page + "&pageSize=" + pageSize + "&keyword=" +keyword;
    $.ajax({
        url: url,
        type: 'GET',
        success: function(response) {
            var postPageInfo = response.data;
            console.log(postPageInfo);
            var posts = postPageInfo.list;
            var postList = $('#postList');
            postList.empty(); // 清空先前的内容
            if (posts.length === 0) {
                // 添加暂无数据的样式
                postList.append('<li class="list-group-item text-center" style="font-size: larger; color: rgba(0, 0, 0, 0.5);">暂无数据</li>');
            } else {
                $.each(posts, function(index, post) {
                    var listItem = $('<li class="list-group-item"></li>');
                    var title = $('<h5 class="post-title"></h5>').text(post.title).css('color', 'blue').css('font-size', 'larger').click(function() {
                        selectPostInfo(post.id)
                    });
                    var content = $('<p class="post-content"></p>').text(post.content);
                    var userInfo = $('<p class="post-user-info col-md-6"></p>').text('发布人：' + post.user.username);
                    var publishTime = $('<p class="post-publish-time col-md-6 text-right"></p>').text('发布时间：' + new Date(post.createTime).toLocaleString());
                    var userInfoContainer = $('<div class="row"></div>').append(userInfo, publishTime);
                    listItem.append(title, content, userInfoContainer);
                    postList.append(listItem);
                });
            }
        },
        error: function() {
            alert('加载帖子失败');
        }
    });

}


// 发帖表单提交
function sendPost(userId,categoryId) {
    var postTitle = $('#postTitle').val();
    var postContent = $('#postContent').val();// 获取所选分类的 ID

    // 检查标题是否为空
    if (!postTitle.trim()) {
        alert('标题不能为空');
        return; // 如果标题为空，退出函数
    }

    // 发送发帖请求
    $.ajax({
        url: 'postServlet',
        method: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({
            title: postTitle,
            content: postContent,
            userId: userId, // 包含用户ID
            categoryId: categoryId // 包含分类ID
        }),
        success: function(data) {
            alert('发帖成功');
            // 可以在这里重新加载帖子列表
        },
        error: function() {
            alert('发帖失败');
        }
    });
};

//查询用户
function selectUser(userId){
    $.ajax({
        url: '/userServlet?userId=' + userId,
        method: 'GET',
        dataType: 'json',
        success: function(data) {
            var data = data.data;
            console.log(data)
            $('#userId').val(data.id);
            $('#username').val(data.username);
            $('#nickname').val(data.nickname);
            $('#bio').val(data.bio);
            $('#gender').val(data.gender);
            $('#phone').val(data.phone);
            $('#email').val(data.email);
            $('#password').val(data.password);
            $('#avatarUrl').attr('src', data.avatarUrl);


        },
        error: function() {
            alert('无法加载用户信息');
        }
    });
}

function updateUser(user) {
    // 处理文件上传
    const file = $('#avatarFile')[0].files[0];
    if (file) {
        const formData = new FormData();
        formData.append('avatarFile', file);
        $.ajax({
            url: '/userServlet', // 上传文件的服务端接口
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(response) {
                console.log(response.data)
                $('#avatarUrl').attr('src', response.data); // 从服务器返回的头像地址
            },
            error: function(xhr, status, error) {
                console.error('上传头像失败', error);
            }
        });
    }
}

function sendUpdateRequest(updatedUserInfo){
console.log(updatedUserInfo)
    $.ajax({
        url: '/userServlet',
        method: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(updatedUserInfo),
        success: function() {
            alert('用户信息更新成功');
            $('#personalCenterModal').modal('hide');
        },
        error: function() {
            alert('用户信息更新失败');
        }
    });
}


//查询详情
function selectPostInfo(postId) {
    window.location.href = '/PostCommentServlet?postId=' + postId; // 例子：直接跳转到详情
}